<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <div th:replace="common/common::html"></div>
    <meta charset="UTF-8">
    <title th:text="#{organ.info}">Title</title>
    <script th:inline="javascript">
        var _gridWidth;
        var _gridHeight;
        //页面自适应
        function resizePageSize(){
            _gridWidth = $(document).width()-12;/*  -189 是去掉左侧 菜单的宽度，   -12 是防止浏览器缩小页面 出现滚动条 恢复页面时  折行的问题 */
            _gridHeight = $(document).height()-32-60; /* -32 顶部主菜单高度，   -90 查询条件高度*/
        }
        $(function () {
            resizePageSize();
            var _columnWidth = _gridWidth/5;
            $("#flexigrid-id").flexigrid({
                width : _gridWidth,
                height : _gridHeight,
                url : /*[[@{getOrganInfolistByPage}]]*/"",
                dataType : 'json',
                colModel : [
                    {display : 'SearchOrganCode',name : 'organUuid',width : 150,sortable : false,align : 'center',hide : 'true'},
                    {display : /*[[#{organ.organName}]]*/"",name : 'organName',width : _columnWidth, sortable : true,align : 'center'},
                    {display : /*[[#{organ.parentUuid}]]*/"",name : 'parentUuid',width : _columnWidth, sortable : true,align : 'center'},
                    {display : /*[[#{organ.DESCRIPTION}]]*/"",name : 'DESCRIPTION',width : _columnWidth, sortable : true,align : 'center'},
                    {display : /*[[#{common.operation}]]*/"",name : 'organUuid',width : _columnWidth, sortable : true,align : 'center',process: function (v, _trid, _row) {
                            var htmlContent = '<a href="#" onclick="deleteUser(\''+ v + '\')"><i class="glyphicon glyphicon-minus" style="color: #2f332a"></i></a>';
                            return htmlContent
                        }}
                ],
                resizable : false, //resizable table是否可伸缩
                useRp : true,
                usepager : true, //是否分页
                autoload : false, //自动加载，即第一次发起ajax请求
                hideOnSubmit : true, //是否在回调时显示遮盖
                showcheckbox : true, //是否显示多选框
                rowhandler : rowDbclick, //是否启用行的扩展事情功能,在生成行时绑定事件，如双击，右键等
                rowbinddata : true
            });
            queryBtn();

            $("#query-btn-id").click(function (event) {
                event.stopPropagation();
                queryBtn();


            })
            $("#confirm-user-del-id").dialog({
                autoOpen : false,
                width : 400,
                modal : true,
                resizable : false,
                title: /*[[#{organ.delete.tip.titile}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var organUuid = $("#confirm-user-del-id").attr("organUuid");
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType: 'json',
                                url: /*[[@{delOrganByUuid}]]*/'',
                                data: [{name: "organUuid", value: organUuid}],
                                success: function (data) {
                                    message(data.msg);
                                    $("#confirm-user-del-id").dialog( "close" );
                                    queryBtn();
                                },
                                error: function (msg) {
                                    message(/*[[#{common.error}]]*/'');
                                }
                            });

                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    },
                ]
            });

            $("#allocate-dialog-id").dialog({
                autoOpen : false,
                width : 400,
                modal : true,
                resizable : false,
                title: /*[[#{user.role.allocate}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var userUuid = $("#allocate-dialog-id").attr("userUUid");
                            var roleUuids = [];
                            var ztreeObj = $.fn.zTree.getZTreeObj("allocate-user-ztree");
                            var nodes = ztreeObj.getCheckedNodes(true);
                            $(nodes).each(function (index,item) {
                                roleUuids.push(item.id);
                            });

                            $.ajax({
                                    type: 'post',
                                    async: false,
                                    dataType : 'json',
                                    url: /*[[@{saveRoleByUserId}]]*/'',
                                    data: [
                                        {name:"userUuid",value:userUuid},
                                        {name:"roleUuids",value:roleUuids}
                                    ],
                                    success: function (data) {
                                        message(data.msg);
                                        $("#allocate-dialog-id").dialog('close');
                                    },
                                    error: function(msg){
                                        message(/*[[#{common.error}]]*/'');
                                    }
                                }
                            );



                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    },
                ]
            });


        });//ready

        function queryBtn() {
            var SearchOrganName = $("#SearchOrganName").val();
            var SearchOrganCode = $("#SearchOrganCode").val();
            var SearchOrganType = $("#SearchOrganType").val();
            var SearchOrganDESCRIPTION = $("#SearchOrganDESCRIPTION").val();
            $("#flexigrid-id").flexOptions({
                extParam:[
                    {name:"SearchOrganName",value:SearchOrganName},
                    {name:"SearchOrganCode",value:SearchOrganCode},
                    {name:"SearchOrganType",value:SearchOrganType},
                    {name:"SearchOrganDESCRIPTION",value:SearchOrganDESCRIPTION}
                ]
            }).flexReload();

        }
        function deleteUser(organUuid) {
            $("#confirm-user-del-id").attr("organUuid",organUuid);
            $("#confirm-user-del-id").dialog('open');
        }
        var allocateSetting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };

        function allocateZtree() {
            $.ajax({
                    type: 'post',
                    async: false,
                    dataType : 'json',
                    url: /*[[@{getOrganZtreeList}]]*/'',
                data: [],
                    success: function (data) {
                        $.fn.zTree.init($("#allocate-user-ztree"), allocateSetting, data);
                        $("#allocate-dialog-id").dialog('open');
                    },
                    error: function(msg){
                        message(/*[[#{common.error}]]*/'');
                    }
                }
            );
        }
        function organAdd() {
            let SearchOrganName = $("#SearchOrganName").val();
            let SearchOrganCode = $("#SearchOrganCode").val();
            let SearchOrganType = $("#SearchOrganType").val();
            let SearchOrganDESCRIPTION = $("#SearchOrganDESCRIPTION").val();
            $.ajax({
                    type: 'post',
                    async: false,
                    dataType : 'json',
                    url: /*[[@{insertOrgan}]]*/'',
                    data: [
                        {name:"organUuid",value:SearchOrganCode},
                        {name:"organName",value:SearchOrganName},
                        {name:"parentUuid",value:SearchOrganType},
                        {name:"DESCRIPTION",value:SearchOrganDESCRIPTION}],
                    success: function (data) {
                        message(/*[[#{organ.insert.success}]]*/'');
                    },
                    error: function(msg){
                        message(/*[[#{organ.insert.error}]]*/'');
                    }
                }
            );
        }
        function updateOrgan() {
            let ids = $("#flexigrid-id").getFlexigridiAttrByIndex(0)
            if(ids.length !== 1){
                message(/*[[#{common.select.one}]]*/'');
                return;
            }
            let SearchOrganName = $("#SearchOrganName").val();
            let SearchOrganCode = $("#SearchOrganCode").val();
            let SearchOrganType = $("#SearchOrganType").val();
            let SearchOrganDESCRIPTION = $("#SearchOrganDESCRIPTION").val();


            $.ajax({
                    type: 'post',
                    async: false,
                    dataType : 'json',
                    url: /*[[@{updateOrgan}]]*/'',
                    data: [
                        {name:"organUuid",value:ids},
                        {name:"organUuidUpdate",value:SearchOrganCode},
                        {name:"organName",value:SearchOrganName},
                        {name:"parentUuid",value:SearchOrganType},
                        {name:"DESCRIPTION",value:SearchOrganDESCRIPTION}],
                    success: function (data) {

                        message(/*[[#{organ.update.success}]]*/'');
                    },
                    error: function(msg){
                        message(/*[[#{common.error}]]*/'');
                    }
                }
            );

        }
        function unbound() {
            let ids = $("#flexigrid-id").getFlexigridiAttrByIndex(0)
            if(ids.length !== 1){
                message(/*[[#{common.select.one}]]*/'');
                return;
            }



            $.ajax({
                    type: 'post',
                    async: false,
                    dataType : 'json',
                    url: /*[[@{unboundByUuid}]]*/'',
                    data: [
                        {name:"organUuid",value:ids},
                        ],
                    success: function (data) {

                        message(/*[[#{organ.unbound.success}]]*/'');
                    },
                    error: function(msg){
                        message(/*[[#{common.error}]]*/'');
                    }
                }
            );

        }
        $("#user-detail-dialog-id").dialog({
            autoOpen : false,
            width : 400,
            modal : true,
            resizable : false,
            title: /*[[#{organ.info}]]*/'',
            buttons: [
                {
                    text: /*[[#{common.confirm}]]*/'',
                    click: function() {
                        $( this ).dialog( "close" );
                    }
                }
            ]
        });

        function rowDbclick(r){
            $(r).dblclick(
                function() {
                    var columnsArray = $(r).attr('ch').split("_FG$SP_");
                    console.info(columnsArray);
                    $("#detail-organ-organUuid-id").text(columnsArray[0]);
                    $("#detail-organ-organName-id").text(columnsArray[1]);
                    $("#detail-organ-parent-id").text(columnsArray[2]);
                    $("#detail-organ-DESCRIPTION-id").text(columnsArray[3]);

                    $("#organ-detail-dialog-id").dialog({
                        buttons: [
                            {
                                text: "Ok",
                                icons: {
                                    primary: "ui-icon-heart"
                                },
                                click: function() {
                                    $( this ).dialog("close");
                                }
                            }
                        ]
                    });
                });
        }


    </script>
</head>
<body>
<div>
    <span th:text="#{organ.info}"></span>
    <div>
        <label  th:text="#{organ.organUuid}"></label> <input type="text" id="SearchOrganCode"/>
        <label th:text="#{organ.organName}"></label> <input type="text" id="SearchOrganName"/>
        <label  th:text="#{organ.parentUuid}"></label> <input type="text" id="SearchOrganType"/>
        <label  th:text="#{organ.DESCRIPTION}"></label> <input type="text" id="SearchOrganDESCRIPTION"/>
    </div>
    <div>
        <a  class="btn" href="#" onclick="queryBtn();">
            <i class="glyphicon glyphicon-zoom-in"></i>
            <span th:text="#{organ.select}"></span>
        </a>
        <a  class="btn" href="#" onclick="queryBtn();">
            <i class="glyphicon glyphicon-resize-small"></i>
            <span th:text="#{organ.bingding}"></span>
        </a>
        <a  class="btn" href="#" onclick="unbound();">
            <i class="glyphicon glyphicon-resize-full"></i>
            <span th:text="#{organ.unbound}"></span>
        </a>
        <a  class="btn" href="#" onclick="updateOrgan();">
            <i class="glyphicon glyphicon-retweet"></i>
            <span th:text="#{organ.update}"></span>
        </a>
        <a  class="btn" href="#" onclick="organAdd();">
            <i class="glyphicon glyphicon-plus"></i>
            <span th:text="#{organ.add}"></span>
        </a>
    </div>



    <div>
        <ul class="nav navbar-nav fa-ul" >
            <li >
                <a href="#" onclick="allocateZtree();">
                    <i class="glyphicon glyphicon-wrench"></i>
                    <span th:text="#{organ.ztree}"></span>
                </a>
            </li>
        </ul>
    </div>

</div>

<table id="flexigrid-id"></table>
<div id="confirm-user-del-id" style="display: none">
    <span th:text="#{organ.delete.tip.info}"></span>
</div>

<div id="allocate-dialog-id" style="display: none">
    <ul id="allocate-user-ztree" class="ztree">

    </ul>

</div>
<div  id="organ-detail-dialog-id" style="display: none">
    <div>
        <span th:text="#{organ.organUuid}"></span>:
        <span id="detail-organ-organUuid-id"></span>
    </div>
    <div>
        <span th:text="#{organ.organName}"></span>:
        <span id="detail-organ-organName-id"></span>
    </div>
    <div>
        <span th:text="#{organ.parentUuid}"></span>:
        <span id="detail-organ-parent-id"></span>
    </div>
    <div>
        <span th:text="#{organ.DESCRIPTION}"></span>:
        <span id="detail-organ-DESCRIPTION-id"></span>
    </div>
</div>

</body>
</html>